//-----------------------------------------------------------------------
// <copyright file="SplitBuildNumber.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Andy Reeves</author>
// <email>andyr</email>
// <date>2005-03-22</date>
// <summary>Splits a string into build number parts.</summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.VersionNumber
{
    using System;
    using System.IO;
    using System.Xml;
    using System.Globalization;
    using System.Text;
    using System.CodeDom;
    using System.CodeDom.Compiler;
    using System.Reflection;

    using Microsoft.Build.Framework;


    public class SplitBuildNumber : TaskBase
    {
        private string revisionBuildNumber;
        private string buildBuildNumber;
        private string buildNumberFull;
        private string buildConfiguration;

        /// <summary>
        /// Initializes a new instance of the SplitBuildNumber class.
        /// </summary>
        public SplitBuildNumber()
        {
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            if (this.BuildConfigBuildNumberRevisionNumber.Length == 0)
            {
                throw new TaskException("VersionNumber.MissingBuildRevision");
            }
            //split Daily_050517.00002 at the underscore
            string[] firstParts = this.BuildConfigBuildNumberRevisionNumber.Split('_');

            //TODO Check for no underscore and no dots and throw errors
            this.buildConfiguration = firstParts[0];

            string[] numbers = firstParts[1].Split('.');
            this.buildBuildNumber = numbers[0];
            this.revisionBuildNumber = numbers[1];
        }

        /// <summary>
        /// The build number string consisting of Daily_050517.00002
        /// This will be split returning 050517 as the BuildNumber
        /// and 00002 as the Revision Number
        /// Typically these 2 values are passed into the CreateSourceFiles task
        /// </summary>
        [Required]
        public string BuildConfigBuildNumberRevisionNumber
        {
            get { return (this.buildNumberFull == null ? String.Empty : this.buildNumberFull); }
            set { this.buildNumberFull = value; }
        }

        /// <summary>
        /// From Daily_050517.00002 this would return 050517
        /// </summary>
        [Output]
        public string BuildNumber
        {
            get { return (this.buildBuildNumber == null ? String.Empty : this.buildBuildNumber); }
        }

        /// <summary>
        /// From Daily_050517.00002 this would return 00002
        /// </summary>
        [Output]
        public string RevisionNumber
        {
            get { return (this.revisionBuildNumber == null ? String.Empty : this.revisionBuildNumber); }
        }

        /// <summary>
        /// From Daily_050517.00002 this would return Daily
        /// </summary>
        [Output]
        public string BuildConfiguration
        {
            get { return (this.buildConfiguration == null ? String.Empty : this.buildConfiguration); }
        }
    }
}


